/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pnlAccountManagement.java
 *
 * Created on 12-08-2011, 09:40:38
 */
package tuitionmanagement.gui;

import tuitionmanagement.DAO.Accountfee;
import tuitionmanagement.entity.Account;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author NHAN
 */
public class pnlAccountManagement extends javax.swing.JPanel {

    Account acc = new Account();
    Accountfee buz = new Accountfee();
    ArrayList<Account> list;
    int index;
    int selectRow = -1;
    public int confirm;
    ArrayList<Account> lst;
   

    /** Creates new form pnlAccountManagement */
    public pnlAccountManagement() {
        initComponents();
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
        lst = buz.readByAll();
        loadTableAccountDetail(index);
    }

    public void loadTableAccountDetail(ArrayList<Account> lst) {
        Vector vRow = new Vector();
        for (int i = 0; i < lst.size(); i++) {
            Vector v = new Vector();
            acc = lst.get(i);
            v.addElement(acc.getAccountId());
            v.addElement(acc.getUsername());
            v.addElement(acc.getPassword());
            v.addElement(acc.getRoleId());
            vRow.addElement(v);
        }
        Vector vCol = new Vector();
        vCol.addElement("AccountId");
        vCol.addElement("Username");
        vCol.addElement("Password");
        vCol.addElement("Role");
        DefaultTableModel tblModel = new DefaultTableModel(vRow, vCol);
        tblAccount.setModel(tblModel);
    }

    public void loadAllactiAccountDetail() {
        lst = buz.readByAll();
        loadTableAccountDetail(lst);
    }

    private void loadTableAccountDetail(int index) {
        ArrayList<Account> lst = buz.readByAll();
        for (int i = 0; i < lst.size(); i++) {
            if (index != lst.get(i).getAccountId()) {
                lst.remove(i);
                i--;
            }
        }
        loadAllactiAccountDetail();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pnlTitle = new javax.swing.JPanel();
        lblTitle = new javax.swing.JLabel();
        pnlRole = new javax.swing.JPanel();
        lblUsername = new javax.swing.JLabel();
        lblAccount = new javax.swing.JLabel();
        txtAccountID = new javax.swing.JTextField();
        lblComment = new javax.swing.JLabel();
        lblPassword = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        txtPassword = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        txtConfirmPass = new javax.swing.JPasswordField();
        txtRole = new javax.swing.JLabel();
        cbxRole = new javax.swing.JComboBox();
        pnlList = new javax.swing.JPanel();
        lblListComment = new javax.swing.JLabel();
        scpGrade = new javax.swing.JScrollPane();
        tblAccount = new javax.swing.JTable();
        pnlFunction = new javax.swing.JPanel();
        btnCancel = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();
        pnlManage = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();

        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });
        setLayout(new java.awt.GridBagLayout());

        pnlTitle.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pnlTitle.setLayout(new java.awt.GridBagLayout());

        lblTitle.setFont(new java.awt.Font("Tahoma", 0, 18));
        lblTitle.setForeground(new java.awt.Color(0, 51, 153));
        lblTitle.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        lblTitle.setText("ACCOUNT MANAGEMENT");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        pnlTitle.add(lblTitle, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.insets = new java.awt.Insets(15, 10, 10, 10);
        add(pnlTitle, gridBagConstraints);

        pnlRole.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Role Management", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        pnlRole.setLayout(new java.awt.GridBagLayout());

        lblUsername.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblUsername.setText("Username");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 50, 5, 5);
        pnlRole.add(lblUsername, gridBagConstraints);

        lblAccount.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblAccount.setText("AccountID");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 50, 5, 5);
        pnlRole.add(lblAccount, gridBagConstraints);

        txtAccountID.setBackground(new java.awt.Color(233, 229, 214));
        txtAccountID.setFont(new java.awt.Font("Tahoma", 0, 12));
        txtAccountID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtAccountIDActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 0.2;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 200);
        pnlRole.add(txtAccountID, gridBagConstraints);

        lblComment.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblComment.setText("Information of relate family that will to be saved into database");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.insets = new java.awt.Insets(20, 20, 5, 20);
        pnlRole.add(lblComment, gridBagConstraints);

        lblPassword.setFont(new java.awt.Font("Tahoma", 0, 12));
        lblPassword.setText("Password");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 50, 5, 5);
        pnlRole.add(lblPassword, gridBagConstraints);

        txtUsername.setFont(new java.awt.Font("Tahoma", 0, 12));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 0.4;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 200);
        pnlRole.add(txtUsername, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 200);
        pnlRole.add(txtPassword, gridBagConstraints);

        jLabel1.setText("Confirm Password:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 50, 5, 5);
        pnlRole.add(jLabel1, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 200);
        pnlRole.add(txtConfirmPass, gridBagConstraints);

        txtRole.setText("Role:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 50, 5, 5);
        pnlRole.add(txtRole, gridBagConstraints);

        cbxRole.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Administrator", "Staff", "Student" }));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 200);
        pnlRole.add(cbxRole, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        add(pnlRole, gridBagConstraints);

        pnlList.setForeground(new java.awt.Color(255, 255, 255));
        pnlList.setLayout(new java.awt.GridBagLayout());

        lblListComment.setFont(new java.awt.Font("Tahoma", 0, 14));
        lblListComment.setForeground(new java.awt.Color(0, 51, 153));
        lblListComment.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblListComment.setText("Account List");
        lblListComment.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(10, 5, 5, 5);
        pnlList.add(lblListComment, gridBagConstraints);

        tblAccount.setFont(new java.awt.Font("Tahoma", 0, 12));
        tblAccount.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "AccountID", "Username", "Password", "Role"
            }
        ));
        tblAccount.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        tblAccount.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblAccountMouseClicked(evt);
            }
        });
        scpGrade.setViewportView(tblAccount);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlList.add(scpGrade, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 50;
        gridBagConstraints.ipady = 166;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.4;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 10);
        add(pnlList, gridBagConstraints);

        pnlFunction.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Function", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N
        pnlFunction.setLayout(new java.awt.GridBagLayout());

        btnCancel.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/delete2.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 60;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 15, 5);
        pnlFunction.add(btnCancel, gridBagConstraints);

        btnSave.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/save_as.png"))); // NOI18N
        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 60;
        gridBagConstraints.insets = new java.awt.Insets(5, 15, 15, 50);
        pnlFunction.add(btnSave, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.ipadx = 2;
        gridBagConstraints.weightx = 0.6;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 5);
        add(pnlFunction, gridBagConstraints);

        pnlManage.setLayout(new java.awt.GridBagLayout());

        btnAdd.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/add2.png"))); // NOI18N
        btnAdd.setText("Add New Account");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlManage.add(btnAdd, gridBagConstraints);

        btnDelete.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/delete2.png"))); // NOI18N
        btnDelete.setText("Delete Account");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlManage.add(btnDelete, gridBagConstraints);

        btnEdit.setFont(new java.awt.Font("Tahoma", 0, 12));
        btnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tuitionmanagement/icon/edit.png"))); // NOI18N
        btnEdit.setText("Edit Account");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.3;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        pnlManage.add(btnEdit, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.4;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 10);
        add(pnlManage, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents

    private void tblAccountMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblAccountMouseClicked
        // TODO add your handling code here:
        int row = tblAccount.getSelectedRow();
        if (row < 0) {
            return;
        }
        txtAccountID.setText(tblAccount.getValueAt(row, 0).toString());
        txtUsername.setText(tblAccount.getValueAt(row, 1).toString());
        btnDelete.setEnabled(true);
        btnEdit.setEnabled(true);
        btnSave.setEnabled(false);


        if (String.valueOf(tblAccount.getValueAt(row, 3)).equalsIgnoreCase("1")) {
            cbxRole.setSelectedIndex(0);
        } else if (String.valueOf(tblAccount.getValueAt(row, 3)).equalsIgnoreCase("2")) {
            cbxRole.setSelectedIndex(1);
        } else {
            cbxRole.setSelectedIndex(2);
        }
        loadAllactiAccountDetail();
        tblAccount.updateUI();
    }//GEN-LAST:event_tblAccountMouseClicked

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:              
        txtAccountID.setText("");
        txtAccountID.setText("");
        txtPassword.setText("");
        txtConfirmPass.setText("");
        txtUsername.setText("");



    }//GEN-LAST:event_btnCancelActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        // TODO add your handling code here:
        String pass = new String(txtPassword.getPassword());
        String confirmpass = new String(txtConfirmPass.getPassword());
        if (!btnEdit.isEnabled()) {

            if (txtUsername.getText().trim().equals("")
                    || String.valueOf(txtPassword.getPassword()).equals("")
                    || String.valueOf(txtConfirmPass.getPassword()).equals("") ||txtAccountID.getText().trim().equals("")) {
                JOptionPane.showMessageDialog(this, "Please fill in all the blanks");
                txtUsername.requestFocus();
                return;
            }
            if (!pass.equals(confirmpass)) {
                JOptionPane.showMessageDialog(this, "Password does not match confirm password!");
                txtPassword.requestFocus();
                return;
            }
            confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "New Account", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
            if (confirm == JOptionPane.YES_OPTION) {

                acc.setAccountId(Integer.parseInt(txtAccountID.getText()));

                acc.setUsername(txtUsername.getText());

                acc.setPassword(String.valueOf(txtPassword.getPassword()));
                if (cbxRole.getSelectedIndex() == 0) {
                    acc.setRoleId(1);
                } else if (cbxRole.getSelectedIndex() == 1) {
                    acc.setRoleId(2);
                } else {
                    acc.setRoleId(3);
                }

                if (buz.create(acc)) {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                    txtAccountID.setText(null);
                    txtUsername.setText(null);
                    txtPassword.setText(null);
                    txtConfirmPass.setText(null);
                    cbxRole.setSelectedIndex(0);
                    txtUsername.requestFocus();
                    btnEdit.setEnabled(false);
                    btnDelete.setEnabled(false);

                    loadAllactiAccountDetail();
                    this.tblAccount.updateUI();
                } else {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                }
            }
        } else {
            txtAccountID.setText(null);
            txtUsername.setText(null);
            txtPassword.setText(null);
            txtConfirmPass.setText(null);
            cbxRole.setSelectedIndex(0);
            txtUsername.requestFocus();
            btnEdit.setEnabled(false);
            btnDelete.setEnabled(false);
        }



    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:

        if (btnSave.isEnabled()) {
            JOptionPane.showMessageDialog(this, "This function is being chosen!");
        } else {
            btnDelete.setEnabled(false);
            btnEdit.setEnabled(false);
            btnSave.setEnabled(true);
            btnCancel.setEnabled(true);
            txtAccountID.setEditable(true);
            txtUsername.setEditable(true);
            txtPassword.setEditable(true);
            txtConfirmPass.setEditable(true);
            cbxRole.setEnabled(true);
            txtAccountID.setText("");
            txtPassword.setText("");
            txtConfirmPass.setText("");
            txtUsername.setText("");
            cbxRole.setSelectedIndex(0);
        }

    }//GEN-LAST:event_btnAddActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        // int selectedRow = tblAccount.getSelectedRow();

        if (txtAccountID.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Please choose an account from the table!");
        } else {
            int accId = Integer.parseInt(txtAccountID.getText());
            confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "Delete Account", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
            if (confirm == JOptionPane.YES_OPTION) {
                if (buz.remove(accId)) {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                    txtAccountID.setText(null);
                    txtUsername.setText(null);
                    txtPassword.setText(null);
                    txtConfirmPass.setText(null);
                    cbxRole.setSelectedIndex(0);
                    btnEdit.setEnabled(true);
                    btnDelete.setEnabled(true);
                    loadAllactiAccountDetail();
                    this.tblAccount.updateUI();
                } else {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                }
            }

        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        // int selectedRow = tblAccount.getSelectedRow();

        if (txtAccountID.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Please choose an account from the table!");
        } else {
            txtAccountID.setEditable(true);
            txtConfirmPass.setEditable(true);
            txtPassword.setEditable(true);
            txtUsername.setEditable(true);
            cbxRole.setEnabled(true);
            String pass = new String(txtPassword.getPassword());
            String confirmpass = new String(txtConfirmPass.getPassword());
            if (txtUsername.getText().trim().equals("")
                    || String.valueOf(txtPassword.getPassword()).equals("")
                    || String.valueOf(txtConfirmPass.getPassword()).equals("")) {
                JOptionPane.showMessageDialog(this, "Please fill in all the blanks");
                txtUsername.requestFocus();
                return;
            }
            if (!pass.equals(confirmpass)) {
                JOptionPane.showMessageDialog(this, "Password does not match confirm password!");
                txtPassword.requestFocus();
                return;
            }
            confirm = JOptionPane.showConfirmDialog(this, "Are you sure?", "Update Account", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE, null);
            if (confirm == JOptionPane.YES_OPTION) {
                acc.setAccountId(Integer.parseInt(txtAccountID.getText()));
                acc.setUsername(txtUsername.getText());
                acc.setPassword(String.valueOf(txtPassword.getPassword()));
                if (cbxRole.getSelectedIndex() == 0) {
                    acc.setRoleId(1);
                } else if (cbxRole.getSelectedIndex() == 1) {
                    acc.setRoleId(2);
                } else {
                    acc.setRoleId(3);
                }
                if (buz.store(acc)) {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                    txtAccountID.setText(null);
                    txtUsername.setText(null);
                    txtPassword.setText(null);
                    txtConfirmPass.setText(null);
                    cbxRole.setSelectedIndex(0);
                    btnEdit.setEnabled(true);
                    btnDelete.setEnabled(true);
                    loadAllactiAccountDetail();
                    this.tblAccount.updateUI();
                } else {
                    JOptionPane.showMessageDialog(this, buz.getLastError());
                }
            }
        }
    }//GEN-LAST:event_btnEditActionPerformed

    private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_formMouseClicked

    private void txtAccountIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtAccountIDActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtAccountIDActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbxRole;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lblAccount;
    private javax.swing.JLabel lblComment;
    private javax.swing.JLabel lblListComment;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JPanel pnlFunction;
    private javax.swing.JPanel pnlList;
    private javax.swing.JPanel pnlManage;
    private javax.swing.JPanel pnlRole;
    private javax.swing.JPanel pnlTitle;
    private javax.swing.JScrollPane scpGrade;
    private javax.swing.JTable tblAccount;
    private javax.swing.JTextField txtAccountID;
    private javax.swing.JPasswordField txtConfirmPass;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JLabel txtRole;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
